using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Xwing
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Meteorite : Microsoft.Xna.Framework.GameComponent
    {
        public Model model { get; set; }
        public Matrix matrix { get; set; }
        public Matrix Temp { get; set; }
        public Vector3 position { get; set; }
        public Vector3 rotation { get; set; }
        public int state { get; set; }
        private int axis { get; set; }
        public float size { get; set; }
        Random random;
        private float x, y, z;
        private Vector3 destination;
        private Vector3 temp;
        private float destX, destY, destZ;

        public Meteorite(Game game, Model model, Matrix matrix, Vector3 position, Vector3 rotation, int state, float size, int axis)
            : base(game)
        {
            random = new Random();
            this.model = model;
            this.matrix = matrix;
            this.Temp = matrix;
            this.position = position;
            this.rotation = rotation;
            x = this.rotation.X * 100;
            y = this.rotation.Y * 100;
            z = this.rotation.Z * 100;
            this.state = state;
            this.axis = axis;
            this.size = size;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            rotation = new Vector3(x++, y++, z++);

            if (state == 1)
            {
                orbit(1.0f, 1.0f, 1.0f);
            }

            if (state == 2)
            {
                pathFinder();
            }
            base.Update(gameTime);
        }

        private void orbit(float x, float y, float z)
        {
            if (axis == 0)
                matrix = matrix
                * Matrix.CreateRotationX(MathHelper.ToRadians(x));
            else if (axis == 1)
                matrix = matrix
                * Matrix.CreateRotationY(MathHelper.ToRadians(y));
            else
                matrix = matrix
                * Matrix.CreateRotationZ(MathHelper.ToRadians(z));
        }

        private void pathFinder()
        {
            if (destination == new Vector3(0.0f, 0.0f, 0.0f) || ((int)position.X == (int)destination.X && (int)position.Y == (int)destination.Y && (int)position.Z == (int)destination.Z))
                randomNextDestination();

            if ((int)position.X < (int)destination.X)
            {
                temp = position;
                temp.X += 0.1f;
                position = temp;
            }
            if ((int)position.X > (int)destination.X)
            {
                temp = position;
                temp.X -= 0.1f;
                position = temp;
            }

            if ((int)position.Y < (int)destination.Y)
            {
                temp = position;
                temp.Y += 0.1f;
                position = temp;
            }
            if ((int)position.Y > (int)destination.Y)
            {
                temp = position;
                temp.Y -= 0.1f;
                position = temp;
            }

            if ((int)position.Z < (int)destination.Z)
            {
                temp = position;
                temp.Z += 0.1f;
                position = temp;
            }
            if ((int)position.Z > (int)destination.Z)
            {
                temp = position;
                temp.Z -= 0.1f;
                position = temp;
            }
        }

        private void randomNextDestination()
        {
            destX = random.Next(-10, 10);
            destY = random.Next(-10, 10);
            destZ = random.Next(-10, 10);

            destination = new Vector3((int)destX, (int)destY, (int)destZ);
        }
    }
}
